<?php

// $Id: config_forms.inc,v 1.1.2.13 2008/09/19 00:05:29 mercmobily Exp $

/**
 * The standard "settings" function
 *
*/
function drigg_admin_settings() {

   $form['aliases_mass_recalculation'] = array(
    '#weight' => -5,
    '#type' => 'submit',
    '#value' => t('Run the mass-recalculation for category aliases'),
    '#title' => t('Recalculates the section alias for each node'),
    '#description' => t('Run this if you change the name of a section in your site'),
   );

  $form['votingapi_info'] = array(
    '#type' => 'fieldset',
    '#title' => t('VotingAPI votes information'),
    '#description' => t('Configure what VotingAPI votes are taken into consideration'),
  );


  $form['drigg_anonymous_can_create_entries'] = array(
    '#type' => 'checkbox',
    '#title' => t("Allow anonymous posting of Drigg stories. If ticked, anonymous users will be able to submit stories. Please note that if this is not ticked, anonymous users will be redirected to the /login page. However, in terms of Drupal permissions, it's important to allow anonymous users to submit Drigg stories, even though Drigg might not allow it (depending on this flag)"),
    '#default_value' => variable_get('drigg_anonymous_can_create_entries', FALSE),
    '#required' => FALSE );

  $form['votingapi_info']['drigg_votingapi_content_type']=array(
    '#type' => 'textfield',
    '#title' => t('The content type which will be considered. Default to "node"'),
    '#size' => 20,
    '#maxlength' => 20,
    '#default_value' => variable_get('drigg_votingapi_content_type', 'node'),
  );

  $form['votingapi_info']['drigg_votingapi_value_type']=array(
    '#type' => 'textfield',
    '#title' => t('The value type which will be considered. Default to "points"'),
    '#size' => 20,
    '#maxlength' => 20,
    '#default_value' => variable_get('drigg_votingapi_value_type', 'points'),
  );

  $form['votingapi_info']['drigg_votingapi_tag']=array(
    '#type' => 'textfield',
    '#title' => t('The content tag which will be considered. Default to "vote"'),
    '#size' => 20,
    '#maxlength' => 20,
    '#default_value' => variable_get('drigg_votingapi_tag', 'vote'),
  );

  $form['votingapi_info']['drigg_votingapi_function']=array(
    '#type' => 'textfield',
    '#title' => t('The content function which will be considered. Default to "sum"'),
    '#size' => 20,
    '#maxlength' => 20,
    '#default_value' => variable_get('drigg_votingapi_function', 'sum'),
  );


  // Get the vocabulary list for the "Section" menu
  $result = db_query('SELECT vid,name FROM {vocabulary} where tags=0');
  while ($vocabulary = db_fetch_object($result) ) {
    $vocabulary_list[$vocabulary->vid]=$vocabulary->name;
  }
  if (sizeof($vocabulary_list) == 0) {
    $vocabulary_list[0]="Please create a category in your Drupal system!";
  }
  $form['drigg_section_vid'] = array(
    '#type' => 'select',
    '#title' => t('The vocabulary that will be used to define sections'),
    '#options' => $vocabulary_list,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_section_vid', 0),
   );

  // Get the vocabulary list for the "Section" menu
  $vocabulary_list = array();
  $result = db_query('SELECT vid,name,tags FROM {vocabulary} where tags=1');
  while ($vocabulary = db_fetch_object($result) ) {
    $vocabulary_list[$vocabulary->vid] = $vocabulary->name;
  }
  if (sizeof($vocabulary_list) == 0) {
    $vocabulary_list[0]="Please create a category in your Drupal system!";
  }
  $form['drigg_tag_vid'] = array(
    '#type' => 'select',
    '#title' => t('The vocabulary which will include the tags'),
    '#options' => $vocabulary_list,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_tag_vid', 0),
  );



  $result = db_query('SELECT format,name FROM {filter_formats}');
  while ($filter = db_fetch_object($result) ) {
    $filter_list[$filter->format]=$filter->name;
  } 
    
  $form['drigg_format_id'] = array(
    '#type' => 'select',
    '#title' => t('The default filter (scoops will always be forced to this format ID)'),
    '#options' => $filter_list,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_format_id', 1),
   );

  $form['drigg_disallowed_term_id'] = array(
    '#type' => 'textfield',
    '#title' => t('The drupal term id that won\'t be accepted as a valid category '),
    '#description' => t('This is handy in case you want to make sure people actually pick a category. Just create a fake section, and write its id here'),
    '#size' => 6,
    '#maxlength' => 6,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_disallowed_term_id', '0'),
   );

  $form['drigg_title_minimum_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The title\'s minimum length'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_title_minimum_length', '10'),
   );
  $form['drigg_title_maximum_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The title\'s maximum length'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_title_maximum_length', '70'),
   );

  $form['drigg_url']=array(
    '#type' => 'fieldset',
    '#title' => t('Settings for shortened URLs'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

  $form['drigg_url']['drigg_separator_short_urls'] = array(
    '#type' => 'textfield',
    '#title' => t('The separator in story URLs'),
    '#description' => t('This is going to be used instead of the space in short urls'),
    '#size' => 1,
    '#maxlength' => 1,
    '#required' => FALSE,
    '#default_value' => variable_get('drigg_separator_short_urls', '_'),
   );

  $form['drigg_url']['drigg_separator_categories'] = array(
    '#type' => 'textfield',
    '#title' => t('The separator in categories'),
    '#description' => t('This is going to be used instead of the space in categories. <b>WATCH OUT</b>: if you change this on a working server, the categories will change names. You will need to run a "mass recalculation" of the categories cache in the Drigg config. However, please note that a LOT of your URLs will suddently change! So, any external link, unless you use ModRewrite, will fail! Changing this is NOT recommended for working sites'),
    '#size' => 1,
    '#maxlength' => 1,
    '#required' => FALSE,
    '#default_value' => variable_get('drigg_separator_categories', ''),
   );


  $form['drigg_body_minimum_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The description\'s minimum length'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_body_minimum_length', '50'),
   );
  $form['drigg_body_maximum_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The description\'s maximum length'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_body_maximum_length', '400'),
   );

  $form['drigg_hours_able_to_edit'] = array(
    '#type' => 'textfield',
    '#title' => t('How many hours entries are able to be edited by submitters (set to 0 for indefinite)'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_hours_able_to_edit', '0'),
   );

  $form['drigg_hours_in_upcoming'] = array(
    '#type' => 'textfield',
    '#title' => t('How many hours entries stay in the "upcoming" queue'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_hours_in_upcoming', '48'),
   );

  $form['drigg_banned_urls'] = array(
    '#type' => 'textarea',
    '#rows' => 20,
    '#title' => t('Banned URLs'),
    '#default_value' => variable_get('drigg_banned_urls', ''),
    '#description' => t('One URL per line, Perl regexp syntax'),
  );

  $form['drigg_submit_as_users'] = array(
    '#type' => 'textfield',
    '#title' => t('User IDs a submit-as user can submit as'),
    '#size' => 20,
    '#maxlength' => 255,
    '#required' => FALSE,
    '#default_value' => variable_get('drigg_submit_as_users', ''),
    '#description' => t('This is a comma-separated list'),
  );

  $form['drigg_filter_unpublished_in_tags'] = array(
    '#type' => 'checkbox',
    '#title' => t("Only show promoted drigg stories when viewing a tag"),
    '#description' => t('Leaving this to TRUE is good for SEO, since only promoted stories will get tag-related pagerank'),
    '#default_value' => variable_get('drigg_filter_unpublished_in_tags', TRUE),
    '#required' => FALSE );

  $form['drigg_items_in_main_page'] = array(
    '#type' => 'textfield',
    '#title' => t('How many items should be shown in each page?'),
    '#size' => 5,
    '#maxlength' => 5,
    '#required' => TRUE,
    '#default_value' => variable_get('drigg_items_in_main_page', 20),
   );

  $form['drigg_auto_first_vote'] = array(
    '#type' => 'checkbox',
    '#title' => t("Automatically cast the first vote by the scoop submitter."),
    '#description' => t("Note: Due to a conflict, this feature should be unchecked if you are using
      CCK Imagefield to insert images into scoops."),
    '#default_value' => variable_get('drigg_auto_first_vote', TRUE),
    '#required' => FALSE );

  $form['drigg_url_validate'] = array(
    '#type' => 'checkbox',
    '#title' => t("Check if URL submitted is valid using HTTT Request."),
    '#description' => t("Note: Due to non compliant server, sometimes URL validation may rejects valid URL. If you deactivate URL validation, Drigg will not test URL validity anymore."),
    '#default_value' => variable_get('drigg_url_validate', TRUE),
    '#required' => FALSE );

   $form['trackback'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings for trackbacks'),
    '#description' => t('Trackbacks will allow you to let our bloggers know when a story gets published in your Drigg site. Trackbacks are managed automatically. Pleae note that Drigg only ever deals with sending pings out.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

  $form['trackback']['drigg_trackback_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable trackback'), 
    '#description' => t('This is the "master" switch for trackback'),
    '#default_value' => variable_get('drigg_trackback_enable', TRUE),
    '#required' => FALSE ); 

  $form['trackback']['drigg_trackback_form_field'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable trackback form field'), 
    '#description' => t('If this is checked, the submission form will allow people to specify the trackback field'),
    '#default_value' => variable_get('drigg_trackback_form_field', FALSE),
    '#required' => FALSE ); 

  $form['trackback']['drigg_trackback_autodiscovery'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable trackback autodiscovery mechanism'), 
    '#description' => t('If this is checked, IF the trackback field is empty, the module will attempt the trackback autodiscovery. Note: this means that the URL will actually be retrieved by your server'),
    '#default_value' => variable_get('drigg_trackback_autodiscovery', TRUE),
    '#required' => FALSE ); 

   $form['trackback']['ping'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings for the actual trackback ping request'),
    '#description' => t('What will be sent out to blogs when pinging them. Note: there is no point in sending the article\'s body, since it will most likely parrot whatever was in the original blog entry.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

  $form['trackback']['ping']['drigg_trackback_ping_title'] = array(
    '#type' => 'textfield',
    '#title' => t('The trackback ping title'),
    '#size' => 35,
    '#maxlength' => 60,
    '#default_value' => variable_get('drigg_trackback_ping_title', 'Story added'),
   );

  $form['trackback']['ping']['drigg_trackback_ping_excerpt'] = array(
    '#type' => 'textarea',
    '#rows' => 6,
    '#title' => t('The trackback ping excerpt.'),
    '#default_value' => variable_get('drigg_trackback_ping_excerpt', 'Your story was featured in !site_name! Here is the link to vote it up and promote it: !url'),
    '#description' => t('NOTE that there are special placeholders in use: !url is the URL of the pinging article, !site_name is the site name as configured in Drupal'),
  );

  $form['trackback']['ping']['drigg_trackback_ping_blog_name'] = array(
    '#type' => 'textfield',
    '#title' => t('The trackback ping "blog name". Sites will display this.'),
    '#size' => 35,
    '#maxlength' => 60,
    '#default_value' => variable_get('drigg_trackback_ping_blog_name',
                                   variable_get('site_name', 'Drigg')
                                    ),
   );

   $form['url_handling'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings about having URLs in forms'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

  $form['url_handling']['drigg_url_mode'] = array(
    '#type' => 'radios',
    '#title' => t('How the system will handle URLs'),
    '#default_value' => variable_get('drigg_url_mode', 0),
    '#options' => array(
                    0 => t('URL is required in the form'),
                    1 => t('URL field is in the form, but it can be left empty'),
                    2 => t('URL field is not in the form')
                  ),
    '#required' => TRUE );

   $form['form_help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Help messages in the forms'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

   $form['form_help']['taxonomy_info'] = array(
    '#type' => 'fieldset',
    '#title' => t('Info to appear next to category'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

  $form['form_help']['taxonomy_info']['drigg_taxonomy_explanation'] = array(
    '#type' => 'textarea',
    '#rows' => 7,
    '#title' => t('Explanation next to the categories'),
    '#default_value' => variable_get('drigg_taxonomy_explanation', ''),
    '#description' => t('This will appear in the same fieldset as the tags adn the story category. This will be translated by t().')
  );

   $form['form_help']['taxonomy_info']['drigg_taxonomy_weight']=array(
    '#type' => 'textfield',
    '#title' => t('Weight of the explanation'),
    '#size' => 25,
    '#maxlength' => 50,
    '#default_value' => variable_get('drigg_taxonomy_weight', 10),
    '#required' => FALSE, 
    '#description' => t('This will determine the placement of the category information')
   );

  $form['form_help']['drigg_body_formatting_options'] = array(
    '#type' => 'textarea',
    '#rows' => 6,
    '#title' => t('Drigg format options'),
    '#default_value' => variable_get('drigg_body_formatting_options', ''),
    '#description' => t('This message will be displayed underneath the body field. You can use this message to give instructions to users who are submitting. This will be translated by t(). '),
  );

  $form['drigg_debug'] = array(
    '#type' => 'checkbox',
    '#title' => t("Show debug messages"),
    '#default_value' => variable_get('drigg_debug', FALSE),
    '#required' => FALSE );

  return system_settings_form($form);
}

/**
 * Function to validate the settings
 *
*/
function drigg_admin_settings_validate($form_id, $form_values) {

  // This hook will sneakly intercept the roles recalculation request
  // and will redirect accordingly to the roles mass-recalculation
  // function
  if ($form_values['op'] == t('Run the mass-recalculation for category aliases')) {
    drupal_goto('drigg/aliases_mass_recalculation');
  }

  // This is stored in a Drupal variable so that the URL handler can
  // get to it (the URL handler does NOT have access to the
  // drigg_section_list function...!)
  // 
  variable_set("drigg_section_list", drigg_section_list(TRUE));

  if ($form_values['drigg_section_vid'] == 0) {
    form_set_error('drigg_section_vid', t("Please select a section vid"));
  }

  $urls_a=split("\n", $form_values['drigg_banned_urls']);
  // Check that the banned URL list are valid
  $problems='';
  foreach ($urls_a as $url) {
    if (!drigg_url_valid($url) && $url != '') {
      $problems .= $url .' ';
    }
  }
  if ($problems != '') {
    form_set_error('drigg_banned_urls', t("These blacklisted URLs are not valid: ") . $problems);
  }
}

# CHUNK #1
   /* $form['user_screen'] = array(
    '#type' => 'fieldset',
    '#title' => t('Customising the look of the user tabs'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

   $form['user_screen']['user_commented'] = array(
    '#type' => 'fieldset',
    '#title' => t('Look of the "commented" voting form'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

 $form['user_screen']['user_commented']['drigg_commented_before_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put before the "who voted" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_commented_before_form', ''),
   );

 $form['user_screen']['user_commented']['drigg_commented_after_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put after the "who voted" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_commented_after_form', ''),
   );

   $form['user_screen']['user_who_voted'] = array(
    '#type' => 'fieldset',
    '#title' => t('Look of the "who_voted" voting form'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

 $form['user_screen']['user_who_voted']['drigg_who_voted_before_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put before the "who voted" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_who_voted_before_form', ''),
   );

 $form['user_screen']['user_who_voted']['drigg_who_voted_after_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put after the "who voted" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_who_voted_after_form', ''),
   );

   $form['user_screen']['user_scoops'] = array(
    '#type' => 'fieldset',
    '#title' => t('Look of the "user scoops" voting form'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE
  );

 $form['user_screen']['user_scoops']['drigg_user_scoops_before_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put before the "users scoops" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_user_scoops_before_form', ''),
   );

 $form['user_screen']['user_scoops']['drigg_user_scoops_after_form']=array(
    '#type' => 'textfield',
    '#title' => t('Text to put after the "users scoops" voting form'),
    '#size' => 30,
    '#maxlength' => 128,
    '#default_value' => variable_get('drigg_user_scoops_after_form', ''),
   );
*/
